/**
 * 新增角色
 * @url admin/system/role/sys/add
 * @description 新增角色
 */
module.exports = {
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: '新增成功' };
		
		// 业务逻辑开始-----------------------------------------------------------
		let {
			role_id,
			role_name,
			permission = [],
			comment = ''
		} = data;
		
		// 参数验证
		if (!role_id) {
			return { code: -1, msg: '角色ID不能为空' };
		}
		if (!role_name) {
			return { code: -1, msg: '角色名称不能为空' };
		}
		
		// 检查角色ID是否已存在
		let existRole = await db.collection('uni-id-roles')
			.where({ role_id })
			.get();
		if (existRole.data.length > 0) {
			return { code: -1, msg: '角色ID已存在' };
		}
		
		// 创建角色
		await db.collection('uni-id-roles').add({
			role_id,
			role_name,
			permission,
			comment,
			create_date: Date.now()
		});
		
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}

